package library.assistant.ui.example.contour;

import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.MenuButton;
import javafx.scene.control.MenuItem;
import javafx.scene.paint.Color;
import javafx.scene.paint.CycleMethod;
import javafx.scene.paint.LinearGradient;
import javafx.scene.paint.Stop;
import javafx.scene.shape.Rectangle;

import java.net.URL;
import java.util.ResourceBundle;

/**
 * 等高线晕染颜色选择
 */
public class ContourColorChoiceController implements Initializable {


    @FXML
    Rectangle colorOptionOne;

    @FXML
    Rectangle colorOptionTwo;

    @FXML
    Rectangle colorOptionSelected;

    @FXML
    MenuButton colorSelectBtn;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        initializeValues();
        initializeAction();
    }

    private void initializeValues() {
        //Setting the linear gradient
        Stop[] stops1 = new Stop[] {
                new Stop(0, Color.GREEN),
                new Stop(1, Color.RED)
        };
        LinearGradient gradient1 =
                new LinearGradient(0, 0, 1, 0, true, CycleMethod.NO_CYCLE, stops1);

        colorOptionOne.setFill(gradient1);
        colorOptionOne.setStrokeWidth(3);
        colorOptionOne.setStroke(Color.CADETBLUE);

        Stop[] stops2 = new Stop[] {
                new Stop(0, Color.BLUE),
                new Stop(1, Color.RED)
        };
        LinearGradient gradient2 =
                new LinearGradient(0, 0, 1, 0, true, CycleMethod.NO_CYCLE, stops2);
        colorOptionTwo.setFill(gradient2);
        colorOptionTwo.setStrokeWidth(3);
        colorOptionTwo.setStroke(Color.CADETBLUE);

        colorOptionSelected.setFill(gradient1);
        colorOptionSelected.setStrokeWidth(3);
        colorOptionSelected.setStroke(Color.CADETBLUE);
    }

    private void initializeAction() {
        colorSelectBtn.getItems().forEach(menuItem -> {
           menuItem.setOnAction(event -> {
               MenuItem o = (MenuItem) event.getSource();
               Object node = o.getGraphic();
               if (node instanceof Rectangle){
                   colorOptionSelected.setFill(((Rectangle) node).getFill());
                   colorOptionSelected.setStrokeWidth(3);
                   colorOptionSelected.setStroke(Color.CADETBLUE);
               }
           });
        });
    }
}
